define(["jquery","utils","fileSaver","HeatPoint"], function($,utils,saveAs,HeatPoint){

    function DataManager() {

    };

    $.extend(DataManager.prototype, {
		
		startDownload:function(xMouseClickAreas,xMouseMoveAreas,xKeyPressedAreas,xCharAnalysisAreas){
			if(utils.skipSave) return;
			var pContent  = "";
			var pPoints   = "";
			var pSep      = "**||**";
			var pFileName = document.title + "-" + new Date().toLocaleString();
			var t = this;
			
			if(pFileName == null) return;

			if(xCharAnalysisAreas != null) 
				$.each(xCharAnalysisAreas, function(index, val){
			 		this.updateCharList();
					var pList =  this.heatPointList;

					pPoints = "";
					for(var i = 0; i < pList.length;i++)
						if(pList[i].Count > 0 )
							pPoints = pPoints +pList[i].keyCode + ":" + pList[i].Count +";"; 

						if (pPoints.length >0)
							pContent = pContent + $(this.domObj).attr('id') +";3;"+ pPoints + "\n";
				});

			if(xKeyPressedAreas != null) 
				$.each(xKeyPressedAreas, function(index, val){
				 
					var pList =  this.heatPointList;

					pPoints = "";
					for(var i = 0; i < pList.length;i++)
						if(pList[i].Count > 0 )
							pPoints = pPoints +pList[i].keyCode + ":" + pList[i].Count +";"; 

						if (pPoints.length >0)
							pContent = pContent +$(this.domObj).attr('id') +";4;"+ pPoints + "\n";
				});

			if(xMouseClickAreas != null) 
				$.each(xMouseClickAreas, function(index, val){
				 
					var pList =  this.heatPointList;

					pPoints = "";
					for(var i = 0; i < pList.length;i++)
							pPoints = pPoints +pList[i].x + ":" + pList[i].y + ":" + pList[i].timeStamp +";"; 

						if (pPoints.length >0)
							pContent = pContent + $(this.domObj).attr('id')+";1;"+ pPoints + "\n";
				});
			
			if(xMouseMoveAreas != null) 
				$.each(xMouseMoveAreas, function(index, val){
				 
					var pList =  this.heatPointList;

					pPoints = "";
					for(var i = 0; i < pList.length;i++)
							pPoints = pPoints +pList[i].x + ":" + pList[i].y + ":" + pList[i].timeStamp +";"; 

						if (pPoints.length >0)
							pContent = pContent + $(this.domObj).attr('id') +";2;"+ pPoints + "\n";
				});
			
			pContent = pContent.slice(0, -1);

			var blob = new Blob([pContent], {type: "text/plain;charset=utf-8"});
			saveAs(blob, pFileName+".txt");
			gblnSaved = true;
		},
		loadDataIntoElement:function(xLine,xItem){

			if(xItem == null) return false;
			xItem.dataStored = false;
			var pContent = xLine.split(";");
			var pKeyStroke;

			if(pContent[1] == "3"){

				for(var i = 2; i < pContent.length;i++){

						pKeyStroke = pContent[i].split(":");
						var result = xItem.heatPointList.filter(function( obj ) {
							return obj.keyCode == pKeyStroke[0];

					});

					if(result.length > 0) 
						result[0].setCount(parseInt(pKeyStroke[1]));
					}
			}
			if(pContent[1] == "4" ){
					
				xItem.heatPointList.clearList()
				
				for(var i = 2; i < pContent.length;i++){
					
					pKeyStroke = pContent[i].split(":");

					var result = xItem.heatPointList.filter(function( obj ) {
						return obj.keyCode == pKeyStroke[0];
					});

				if(result.length > 0) 
					result[0].setCount(parseInt(pKeyStroke[1]));
				}

			}
			if(pContent[1] == "1" || pContent[1] == "2"){
				
				var pPoint;
				var pHeatPoint;
				xItem.heatPointList = [];
				xItem.heatmap.clear();
				for(var i = 2; i < pContent.length;i++){
					
					pPoint = pContent[i].split(":");
					pHeatPoint = new HeatPoint(pPoint[0], pPoint[1],pPoint[2]);
					xItem.heatPointList.push(pHeatPoint);

				}
			}
		}
   });
   return DataManager;
});